package com.base.library.bindadapter.view

import android.graphics.Paint
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewStub
import android.widget.AbsSeekBar
import android.widget.ProgressBar
import android.widget.SeekBar
import android.widget.TextView
import androidx.core.graphics.drawable.toDrawable
import androidx.databinding.BindingAdapter
import com.base.library.R
import com.base.library.ext.getDrawable
import com.base.library.utils.FastClickUtil.Companion.canHandleClick
import com.base.library.utils.FastClickUtil.Companion.click
import com.base.library.utils.LogUtils

class ViewExt {
    companion object {
        @JvmStatic
        @BindingAdapter(
            value = ["visible"], requireAll = false
        )
        fun setVisible(view: View, visible: Boolean) {
            view.visibility = if (visible) View.VISIBLE else View.GONE
        }

        @JvmStatic
        @BindingAdapter(
            value = ["drawableStart", "drawableEnd", "drawableTop", "drawableBottom"],
            requireAll = false
        )
        fun setDrawable(
            view: TextView,
            drawableStart: Int = 0,
            drawableEnd: Int = 0,
            drawableTop: Int = 0,
            drawableBottom: Int = 0
        ) {
            view.setCompoundDrawablesWithIntrinsicBounds(
                drawableStart,
                drawableTop,
                drawableEnd,
                drawableBottom
            )
        }

        @JvmStatic
        @BindingAdapter(
            value = ["android:onClick"], requireAll = false
        )
        fun setOnClick(view: View, onClickListener: OnClickListener?) {
            onClickListener?.let {
                view.click(it)
            }
        }

        @JvmStatic
        @BindingAdapter(
            value = ["bgResId"], requireAll = false
        )
        fun setBgDrawable(view: View, bgResId: Int?) {
            if (bgResId == null) {
                view.background = null
                return
            }
            view.background = bgResId.getDrawable()
        }

        @JvmStatic
        @BindingAdapter(
            value = ["isSelected"], requireAll = false
        )
        fun setSelected(view: View, isSelected: Boolean) {
            view.isSelected = isSelected
        }

        @JvmStatic
        @BindingAdapter(
            value = ["android:layout"], requireAll = false
        )
        fun setLayoutId(view: ViewStub, layoutId: Int) {
            if (layoutId <= 0) {
                return
            }
            if (view.layoutResource != layoutId) {
                view.layoutResource = layoutId
                view.inflate()
            }
        }

        /**
         * 设置中划线
         */
        @JvmStatic
        @BindingAdapter(
            value = ["strikeTextFlag"], requireAll = false
        )
        fun setStrikeTextFlag(view: TextView, strike: Boolean?) {
            if (strike == true) {
                //中划线
                view.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
            }
        }


        /**
         * 设置seekbar进度监听
         */
        @JvmStatic
        @BindingAdapter(
            value = ["progressListener"], requireAll = false
        )
        fun setProgressListener(view: SeekBar, listener: SeekBar.OnSeekBarChangeListener?) {
            view.setOnSeekBarChangeListener(listener)
        }
    }
}